
import { get_dict, get_file_ip,get_version } from "@/api/user.js";
//解密加密
import {
	Sm4Dncrypt,
	Sm4encrypt
} from '@/utils/encryption.js'
const state = {
	dictList: sessionStorage.getItem(Sm4encrypt('dict')) ? JSON.parse(Sm4Dncrypt(sessionStorage.getItem(Sm4encrypt('dict')))) ||
		[] : [],
	fileUrl: sessionStorage.getItem(Sm4encrypt('fileUrl')) ? JSON.parse(Sm4Dncrypt(sessionStorage.getItem(Sm4encrypt('fileUrl')))) ||
		"" : "",
	userInfos:sessionStorage.getItem(Sm4encrypt('userInfos')) ? JSON.parse(Sm4Dncrypt(sessionStorage.getItem(Sm4encrypt('userInfos')))) ||
	{} : {},
	itemsObj:sessionStorage.getItem(Sm4encrypt('itemsObj')) ? JSON.parse(Sm4Dncrypt(sessionStorage.getItem(Sm4encrypt('itemsObj')))) ||
	{} : {},
	accountAndPass:localStorage.getItem(Sm4encrypt('accountAndPass')) ? JSON.parse(Sm4Dncrypt(localStorage.getItem(Sm4encrypt('accountAndPass')))) ||
	{} : {},
	listId:sessionStorage.getItem(Sm4encrypt('listId')) ? JSON.parse(Sm4Dncrypt(sessionStorage.getItem(Sm4encrypt('listId')))) ||
	"" : "",
	fromRouter:sessionStorage.getItem(Sm4encrypt('fromRouter')) ? JSON.parse(Sm4Dncrypt(sessionStorage.getItem(Sm4encrypt('fromRouter')))) ||
	"" : "",
	version:sessionStorage.getItem(Sm4encrypt('version')) ? JSON.parse(Sm4Dncrypt(sessionStorage.getItem(Sm4encrypt('version')))) ||
	"" : "",
}

const mutations = {
	SET_DICT: (state, data) => {
		state.dictList = data[0].children
		sessionStorage.setItem(Sm4encrypt("dict"), Sm4encrypt(JSON.stringify(data[0].children)));
	},
	SET_FILE_IP: (state, data) => {
		state.fileUrl = data
		sessionStorage.setItem(Sm4encrypt("fileUrl"), Sm4encrypt(JSON.stringify(data)));
	},
	saveUserInfo:(state, data) => {
		state.userInfos = data
		sessionStorage.setItem(Sm4encrypt("userInfos"), Sm4encrypt(JSON.stringify(data)));
	},
	SET_ITEMS: (state, data) => {
		state.itemsObj = data
		sessionStorage.setItem(Sm4encrypt("itemsObj"), Sm4encrypt(JSON.stringify(data)));
	},
	SET_ID: (state, data) => {
		state.listId = data
		sessionStorage.setItem(Sm4encrypt("listId"), Sm4encrypt(JSON.stringify(data)));
	},
	saveAccount:(state, data) => {
		state.accountAndPass = data
		localStorage.setItem(Sm4encrypt("accountAndPass"), Sm4encrypt(JSON.stringify(data)));
	},
	SET_ROUTER: (state, data) => {
		state.fromRouter = data
		sessionStorage.setItem(Sm4encrypt("fromRouter"), Sm4encrypt(JSON.stringify(data)));
	},
	SET_VERSION: (state, data) => {
		state.version = data
		sessionStorage.setItem(Sm4encrypt("version"), Sm4encrypt(JSON.stringify(data)));
	},
}
/**
 * 格式化后台传过来的json数据为路由格式
 * @param routes asyncRoutes
 * @param token
 */

const actions = {
	getDict({
		commit,
		dispatch
	}) {
		return new Promise((resolve, reject) => {
			get_dict().then(result => {
				if (result.succeed) {
					commit('SET_DICT', result.data)
					
					resolve(true)
				} else {
					resolve(false)
				}
			})
			// 获取文件ip
			get_file_ip().then(result => {
				if (result.succeed) {
					commit('SET_FILE_IP', result.data.fileServerUrl)
					// commit('SET_FILE_IP', 'http://192.168.45.42:8989/')
					// commit('SET_FILE_IP', 'http://192.168.45.42:8989/')// 内网访问文件ip端口
					// commit('SET_FILE_IP', 'http://60.168.132.92:8989/')// 外网访问文件ip端口
					
				}
			})
		})
	},
	getVersion({
		commit,
		dispatch
	}) {
		return new Promise((resolve, reject) => {
			get_version().then(result => {
				if (result.succeed) {
					commit('SET_VERSION', result.data.standard)
					resolve(true)
				} else {
					resolve(false)
				}
			})
		})
	},
	logout({
		commit,
		state
	}) {
		return new Promise((resolve, reject) => {
			commit('SET_ROUTES', []);
			sessionStorage.removeItem('token');
			sessionStorage.removeItem(Sm4encrypt('dict'));
			sessionStorage.removeItem(Sm4encrypt('fileUrl'));

		})
	},
	savePage({ commit }, state) {
		commit('SAVE_PAGE', state)
	},
	saveAppType({ commit }, state) {
		commit('SAVE_APP_TYPE', state)
	},

}


export default {
	namespaced: true,
	state,
	mutations,
	actions
}
